package me.chenqiang.tre.expression;

import java.util.Collection;
import java.util.function.BiFunction;

import me.chenqiang.tre.entity.Card;
import me.chenqiang.tre.entity.Device;

/**
 * 用来表示“包含”或“不包含”的表达式
 * 
 * 表达式左侧的属性是字符串集合，右侧的属性是单个字符串。例如用于判断持卡人身份标签是否具有某个特定标签值。
 * 
 */
public class HasExpression implements Expression{
	public enum Type {
		HAS, HAS_NOT	
	}
	protected Type type;
	protected String propertyName;
	protected BiFunction<Card, Device, Collection<String>> propertyGetter;
	protected String propertyValue;	

	public HasExpression(Type type, String propertyName, BiFunction<Card, Device, Collection<String>> propertyGetter,
			String value) {
		super();
		this.type = type;
		this.propertyName = propertyName;
		this.propertyGetter = propertyGetter;
		this.propertyValue = value;
	}

	@Override
	public boolean evaluate(Card card, Device device) {
		switch(this.type) {
		case HAS:
			return this.propertyGetter.apply(card, device).contains(propertyValue);
		case HAS_NOT:
			return !this.propertyGetter.apply(card, device).contains(propertyValue);
		default:
			throw new IllegalArgumentException(String.format("错误类型：%s", this.type));
		}
		
	}

	@Override
	public String getType() {
		return this.type.name();
	}

	@Override
	public String getPropertyName() {
		return this.propertyName;
	}

	@Override
	public String getPropertyValue() {
		return this.propertyValue;
	}	
	
}
